package com;

import com.fz.entity.*;
import com.fz.mapper.MemberMapper;
import com.fz.mapper.StudentMapper;
import com.fz.mapper.TeacherMapper;
import com.fz.mybatis.MyHelper;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by webrx on 2017-08-28.
 */
public class Demo {
    //一对一演示

    @Test
    public void tt(){
        MyHelper mh = new MyHelper();
        SqlSession s = mh.getSession();
        TeacherMapper tdao = s.getMapper(TeacherMapper.class);
        Teacher tea = tdao.queryById(1);



        System.out.println(tea.getName());
        System.out.println(tea.getId());

        StuClass sc = tea.getSclass();
        System.out.println(sc.getZid());
        System.out.println(sc.getZname());
    }


    //一对多演示
    @Test
    public void zz(){
        MyHelper mh=new MyHelper();
        SqlSession s = mh.getSession();
        MemberMapper mm=s.getMapper(MemberMapper.class);
        Member me=mm.queryById(2);
        System.out.println(me);
        System.out.println(mm.queryByInt(1));
        System.out.println(mm.queryall());
        List<Member> lis=mm.queryall();
        for (Member li:lis){
            System.out.println(li);
        }

        /*List<Order> or=me.getOrders();
        for (Order o:or){
            System.out.println(o);
        }*/
    }


    //动态语句演示
    @Test
    public void dong(){
        MyHelper mh=new MyHelper();
        SqlSession s=mh.getSession();
        StudentMapper stu=s.getMapper(StudentMapper.class);
        List<Student> li=stu.search(null);
        System.out.println(li);
        Map<String,Object> map=new HashMap<String,Object>();
        //根据姓名查询
        //map.put("name","%1%");

        //根据地址和名字查询
        //map.put("address","%东%");
        map.put("name","%1%");
        List<Student> lis=stu.search2(map);
        System.out.println(lis);

        //根据id修改
        /*Map<String,Object> ma=new HashMap<String,Object>();
        ma.put("id",3);
        ma.put("name","李四");
        ma.put("score",20);
        ma.put("address","内蒙古");

        int i=stu.update(ma);
        System.out.println(i);*/
        //用了trim演示
        Map<String,Object> ma=new HashMap<String,Object>();
        ma.put("id",5);
        ma.put("name","李7四");
        ma.put("score",81);
        ma.put("address","西");

        /*int i=stu.update1(ma);
        System.out.println(i);*/

        //统一增加二十分
     /*   int ii=stu.updateInc(20);
        System.out.println(ii);*/


     //同时删除多个指定id
        Map<String,Object> m=new HashMap<String,Object>();
        //根据id删除单个
       // m.put("id",5);
        //根据姓名删除单个
        m.put("name","李2四");
        //删除多个
        //m.put("ids",new int[]{1,2,3,4});
        System.out.println(stu.delete(m));
        s.commit();
    }

    //动态语句修改数据
    @Test
    public void ccc(){
        MyHelper mh=new MyHelper();
        SqlSession s=mh.getSession();
        StudentMapper stu=s.getMapper(StudentMapper.class);
        Map<String,Object> map=new HashMap<String,Object>();
        //本例所做到的效果是，修改id=12和13和14当中姓名有“恒”字的学生的分数减250
        map.put("ids",new int[]{12,13,14});
        map.put("score",-250);
        map.put("name","%恒%");
        System.out.println(stu.updateScore(map));
        s.commit();
    }
}
